﻿/**
 * @file std_vector.inl
 * Copyright (c) 2019 Gaaagaa. All rights reserved.
 * 
 * @author  : Gaaagaa
 * @date    : 2019-09-09
 * @version : 1.0.0.0
 * @brief   : 定义 向量容器 内部相关数据类型、常量、宏 等。
 */

/**
 * The MIT License (MIT)
 * Copyright (c) Gaaagaa. All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef __STD_VECTOR_INL__
#define __STD_VECTOR_INL__

#ifndef __STD_VECTOR_H__
#error "Please include std_vector.h before this file."
#endif // __STD_VECTOR_H__

#include "std_comm.inl"

////////////////////////////////////////////////////////////////////////////////
// xstd_vector_t 的相关数据类型定义

/**
 * @struct xvct_node_t
 * @brief  向量容器所存储的 节点元素对象 的结构体描述信息。
 */
typedef struct xvct_node_t
{
    xstd_byte_t     xvct_elem[0]; ///< 指向节点元素对象的缓存
} xvct_node_t;

/**
 * @struct xstd_vector_t
 * @brief  向量容器的结构体描述信息。
 */
typedef struct xstd_vector_t
{
    xstd_context_t  xstd_ectxt;   ///< 节点类型的上下文描述信息

    /** 向量缓存操作相关的指针表 */
    struct
    {
    xvct_iter_t     xiter_head;   ///< 缓存首端（存储有效节点的起始位置）
    xvct_iter_t     xiter_vend;   ///< 存储有效节点的结束位置
    xvct_iter_t     xiter_tail;   ///< 缓存末端
    } xvct_entry;
} xstd_vector_t;

////////////////////////////////////////////////////////////////////////////////
// xstd_vector_t 的相关操作宏函数

//====================================================================

#define XVCT_ECTXT(xvct_this)   XSTD_ECTXT(xvct_this)
#define XVCT_ETYPE(xvct_this)   XSTD_ETYPE(xvct_this)
#define XVCT_ESIZE(xvct_this)   XSTD_ESIZE(xvct_this)
#define XVCT_ALTOR(xvct_this)   XSTD_ALTOR(xvct_this)

#define XVCT_MALLOC(xvct_this, xst_size)            \
            XSTD_MALLOC((xvct_this), (xst_size))

#define XVCT_DEALLOC(xvct_this, xmt_mptr)           \
            XSTD_DEALLOC((xvct_this), (xmt_mptr))

#define XVCT_ALFROM(xvct_this, xet_dptr, xet_sptr)  \
            XSTD_ALFROM((xvct_this), (xet_dptr), (xet_sptr))

#define XVCT_ALMOVE(xvct_this, xet_dptr, xet_sptr)  \
            XSTD_ALMOVE((xvct_this), (xet_dptr), (xet_sptr))

#define XVCT_ALDEST(xvct_this, xet_elem)            \
            XSTD_ALDEST((xvct_this), (xet_elem))

#define XVCT_ISSAME_TYPE(xvct_lthis, xvct_rthis)    \
            XSTD_ISSAME_TYPE((xvct_lthis), (xvct_rthis))

#define XVCT_ECTXT_UPDATE(xvct_this, xvct_ctxt)     \
            XSTD_ECTXT_UPDATE((xvct_this), (xvct_ctxt))

//====================================================================

#define XVCT_MAX_SIZE(xvct_this) \
    ((xvct_size_t)(((xvct_size_t)~0) / XVCT_ESIZE(xvct_this)))

#define XVCT_ITER_HEAD(xvct_this)   ((xvct_this)->xvct_entry.xiter_head)
#define XVCT_ITER_VEND(xvct_this)   ((xvct_this)->xvct_entry.xiter_vend)
#define XVCT_ITER_TAIL(xvct_this)   ((xvct_this)->xvct_entry.xiter_tail)

#define XVCT_IPTR(xvct_iter)        ((xvct_iter)->xvct_elem)
#define XVCT_HPTR(xvct_this)        XVCT_IPTR(XVCT_ITER_HEAD(xvct_this))
#define XVCT_VPTR(xvct_this)        XVCT_IPTR(XVCT_ITER_VEND(xvct_this))
#define XVCT_TPTR(xvct_this)        XVCT_IPTR(XVCT_ITER_TAIL(xvct_this))

#define XVCT_ITER_BEGIN(xvct_this)  XVCT_ITER_HEAD(xvct_this)
#define XVCT_ITER_END(xvct_this)    XVCT_ITER_VEND(xvct_this)

#define XVCT_ITER_RBEGIN(xvct_this) \
    ((xvct_iter_t)(XVCT_VPTR(xvct_this) - XVCT_ESIZE(xvct_this)))

#define XVCT_ITER_REND(xvct_this)   \
    ((xvct_iter_t)(XVCT_HPTR(xvct_this) - XVCT_ESIZE(xvct_this)))

#define XVCT_ITER_ELEM(xvct_iter)  ((xvct_elem_t)XVCT_IPTR(xvct_iter))

#define XVCT_ITER_PREV(xvct_this, xvct_iter) \
    ((xvct_iter) = (xvct_iter_t)(XVCT_IPTR(xvct_iter) - XVCT_ESIZE(xvct_this)))

#define XVCT_ITER_NEXT(xvct_this, xvct_iter) \
    ((xvct_iter) = (xvct_iter_t)(XVCT_IPTR(xvct_iter) + XVCT_ESIZE(xvct_this)))

#define XVCT_ITER_SEEK(xvct_this, xvct_iter, xst_isize) \
    ((xvct_iter) = (xvct_iter_t)(XVCT_IPTR(xvct_iter) + \
                   ((xvct_ssize_t)(xst_isize) * (xvct_ssize_t)XVCT_ESIZE(xvct_this))))

#define XVCT_ITER_OFFS(xvct_this, xvct_iter, xst_isize) \
    ((xvct_iter_t)(XVCT_IPTR(xvct_iter) +               \
                   ((xvct_ssize_t)(xst_isize) * (xvct_ssize_t)XVCT_ESIZE(xvct_this))))

#define XVCT_ITER_DIST(xvct_this, xiter_near, xiter_far)    \
    ((xvct_size_t)(XVCT_IPTR(xiter_far) - XVCT_IPTR(xiter_near)) / XVCT_ESIZE(xvct_this))

#define XVCT_ITER_VALID(xvct_this, xvct_iter)           \
    ((XVCT_IPTR(xvct_iter) >= XVCT_HPTR(xvct_this)) &&  \
     (XVCT_IPTR(xvct_iter) <= XVCT_VPTR(xvct_this)) &&  \
     (0 == ((XVCT_IPTR(xvct_iter) - XVCT_HPTR(xvct_this)) % XVCT_ESIZE(xvct_this))))

#define XVCT_ITER_HIT(xvct_this, xvct_iter)             \
    ((XVCT_IPTR(xvct_iter) >= XVCT_HPTR(xvct_this)) &&  \
     (XVCT_IPTR(xvct_iter) <  XVCT_VPTR(xvct_this)) &&  \
     (0 == ((XVCT_IPTR(xvct_iter) - XVCT_HPTR(xvct_this)) % XVCT_ESIZE(xvct_this))))

#define XVCT_ITER_INNER(xvct_this, xvct_iter)           \
    ((XVCT_IPTR(xvct_iter) >= XVCT_HPTR(xvct_this)) &&  \
     (XVCT_IPTR(xvct_iter) <  XVCT_VPTR(xvct_this)))

#define XVCT_ITER_OUTER(xvct_this, xvct_iter)           \
    ((XVCT_IPTR(xvct_iter) <  XVCT_HPTR(xvct_this)) ||  \
     (XVCT_IPTR(xvct_iter) >= XVCT_VPTR(xvct_this)))

#define XVCT_RESET_ENTRY(xvct_this)            \
    do                                         \
    {                                          \
        XVCT_ITER_HEAD(xvct_this) = XVCT_NULL; \
        XVCT_ITER_VEND(xvct_this) = XVCT_NULL; \
        XVCT_ITER_TAIL(xvct_this) = XVCT_NULL; \
    } while (0)

#define XVCT_EMPTY(xvct_this) \
    (XVCT_ITER_HEAD(xvct_this) == XVCT_ITER_VEND(xvct_this))

#define XVCT_SIZE(xvct_this) \
    XVCT_ITER_DIST(xvct_this, XVCT_ITER_HEAD(xvct_this), XVCT_ITER_VEND(xvct_this))

#define XVCT_REMAIN(xvct_this) \
    XVCT_ITER_DIST(xvct_this, XVCT_ITER_VEND(xvct_this), XVCT_ITER_TAIL(xvct_this))

#define XVCT_CAPACITY(xvct_this) \
    XVCT_ITER_DIST(xvct_this, XVCT_ITER_HEAD(xvct_this), XVCT_ITER_TAIL(xvct_this))

//====================================================================

////////////////////////////////////////////////////////////////////////////////

#endif // __STD_VECTOR_INL__
